/**
 * Copyright (c) 2020 INESC TEC <https://www.inesctec.pt>
 *
 * This Source Code Form is subject to the terms of the European Union
 * Public License, v. 1.2. If a copy of the EUPL was not distributed with
 * this file, You can obtain one at https://opensource.org/licenses/EUPL-1.2.
 *
 * SPDX-License-Identifier: EUPL-1.2
 */

import React, { useMemo } from 'react';
import { View, StyleSheet, Image } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import PropTypes from 'prop-types';

import { useTheme } from '@app/contexts/Theme';

import { sizes, iconSizes } from '@app/common/theme';

import i18n from '@app/services/i18n';

import TopComponent from '@app/common/components/TopComponent';
import Layout from '@app/common/components/Layout';
import ButtonWrapper from '@app/common/components/ButtonWrapper';
import Text from '@app/common/components/FormattedText';
import Icon from '@app/common/components/Icon';
import List from '@app/common/components/List';

import { getThemedImage } from '@app/common/assets/images';

const styles = (colors, insets) => StyleSheet.create({
  container: {
  },
  closeButton: {
    position: 'absolute',
    top: sizes.size8,
    left: -sizes.size8,
    padding: sizes.size8,
    alignSelf: 'flex-start',
  },
  header: {
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
  },
  layoutContainer: {
    flex: 1,
    backgroundColor: colors.transparent,
    zIndex: 10,
  },
  title: {
    paddingVertical: sizes.size16,
    marginBottom: sizes.size24,
  },
  itemsContainer: {
    marginBottom: sizes.size20,
  },
  imagesContainer: {
    position: 'absolute',
    bottom: 0,
    width: '100%',
    zIndex: 0,
  },
  sponsors: {
    position: 'absolute',
    flexDirection: 'row',
    bottom: sizes.size24 + insets.bottom,
    left: sizes.size24,
    zIndex: 0,
  },
  republicaPortuguesaImage: {
    marginRight: sizes.size24,
  },
  dgsImage: {
  },
  splashImage: {
    alignSelf: 'flex-end',
  },
});


export default function LegalInformation(props) {
  const {
    onClose,
    onPressPrivacyPolicy,
    onPressTermsOfUse,
    onPressTechnicalSheet,
    onPressLicenses,
  } = props;

  const insets = useSafeAreaInsets();
  const { name, colors } = useTheme();
  const memoizedStyle = useMemo(() => styles(colors, insets), [name, insets]);

  const topItems = [
    {
      id: 1,
      title: i18n.translate('screens.legal_information.terms_of_use.label'),
      onPress: onPressTermsOfUse,
      accessibilityLabel: i18n.translate('screens.legal_information.terms_of_use.accessibility.label'),
      accessibilityHint: i18n.translate('screens.legal_information.terms_of_use.accessibility.hint'),
    },
    {
      id: 2,
      title: i18n.translate('screens.legal_information.privacy_policy.label'),
      onPress: onPressPrivacyPolicy,
      accessibilityLabel: i18n.translate('screens.legal_information.privacy_policy.accessibility.label'),
      accessibilityHint: i18n.translate('screens.legal_information.privacy_policy.accessibility.hint'),
    },
  ];

  const bottomItems = [
    {
      id: 1,
      title: i18n.translate('screens.legal_information.technical_sheet.label'),
      onPress: onPressTechnicalSheet,
      accessibilityLabel: i18n.translate('screens.legal_information.technical_sheet.accessibility.label'),
      accessibilityHint: i18n.translate('screens.legal_information.technical_sheet.accessibility.hint'),
    },
    {
      id: 2,
      title: i18n.translate('screens.legal_information.licenses.label'),
      onPress: onPressLicenses,
      accessibilityLabel: i18n.translate('screens.legal_information.licenses.accessibility.label'),
      accessibilityHint: i18n.translate('screens.legal_information.licenses.accessibility.hint'),
    },
  ];

  return (
    <TopComponent style={memoizedStyle.container}>
      <Layout style={memoizedStyle.layoutContainer}>
        <View style={memoizedStyle.header}>
          <ButtonWrapper
            onPress={onClose}
            style={memoizedStyle.closeButton}
            accessibilityLabel={i18n.translate('screens.legal_information.actions.back.accessibility.label')}
            accessibilityHint={i18n.translate('screens.legal_information.actions.back.accessibility.hint')}
          >
            <Icon name='arrow' width={iconSizes.size24} height={iconSizes.size24} />
          </ButtonWrapper>
          <Text size='xlarge' weight='bold' style={memoizedStyle.title}>{i18n.translate('screens.legal_information.title')}</Text>
        </View>
        <List style={memoizedStyle.itemsContainer} items={topItems} />
        <List style={memoizedStyle.itemsContainer} items={bottomItems} />
      </Layout>
      <View style={memoizedStyle.imagesContainer}>
        <View style={memoizedStyle.sponsors}>
          <Image source={getThemedImage('republica_portuguesa', name)} style={memoizedStyle.republicaPortuguesaImage} />
          <Image source={getThemedImage('logo_dgs', name)} style={memoizedStyle.dgsImage} />
        </View>
        <Image source={getThemedImage('splash', name)} style={memoizedStyle.splashImage} />
      </View>
    </TopComponent>
  );
}

LegalInformation.defaultProps = {
  onClose: () => {},
  onPressTermsOfUse: () => {},
  onPressTechnicalSheet: () => {},
  onPressPrivacyPolicy: () => {},
  onPressLicenses: () => {},
};

LegalInformation.propTypes = {
  onClose: PropTypes.func,
  onPressTermsOfUse: PropTypes.func,
  onPressTechnicalSheet: PropTypes.func,
  onPressPrivacyPolicy: PropTypes.func,
  onPressLicenses: PropTypes.func,
};
